<template>
    <div>
        <div>
            <el-input size="small" v-model="jl.name" style="width: 300px;" prefix-icon="el-icon-plus"
                      placeholder="添加职称..."></el-input>
            <el-select v-model="jl.titleLevel" placeholder="职称等级" size="small"
                       style="margin-left: 5px;margin-right: 5px">
                <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item">
                </el-option>
            </el-select>
            <el-button icon="el-icon-plus" type="primary" size="small" @click="addJobLevel">添加</el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="jls"
                    border
                    stripe
                    size="small"
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
                <!--多选框-->
                <el-table-column type="selection" width="55"></el-table-column>

                <el-table-column prop="id" label="编号" width="50"></el-table-column>
                <el-table-column prop="name" label="职称名称" width="180"></el-table-column>
                <el-table-column prop="titleLevel" label="职称级别" width="100"></el-table-column>
                <el-table-column prop="createDate" label="创建时间" width="180"></el-table-column>

                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="small" @click="showEditView(scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" size="small" style="margin-top: 10px" :disabled="multipleSelection.length==0"
                       @click="deleteMany">批量删除
            </el-button>
        </div>

        <el-dialog title="修改职称" :visible.sync="dialogVisible" width="30%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>职称名</el-tag>
                        </td>
                        <td>
                            <el-input size="small" v-model="updateJl.name"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>职称级别</el-tag>
                        </td>
                        <td>
                            <el-select v-model="updateJl.titleLevel" placeholder="职称等级" size="small"
                                       style="margin-left: 5px;margin-right: 5px">
                                <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "JobLevelMana",
        data() {
            return {
                //待添加数据
                jl: {
                    name: '',
                    titleLevel: ''
                },
                //表格数据
                jls: [],

                multipleSelection: [],

                dialogVisible: false,
                //待提交的更新数据
                updateJl: {
                    name: '',
                    titleLevel: '',
                },

                titleLevels: [
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                ]
            }
        },
        mounted() {
            this.initJls();
        },
        methods: {
            deleteMany() {
                this.$confirm('此操作将永久删除【' + this.multipleSelection.length + '】条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    })
                    this.deleteRequest("/system/basic/joblevel/" + ids).then(resp => {
                        if (resp) {
                            this.initJls();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            addJobLevel() {
                if (this.jl.name && this.jl.titleLevel) {
                    this.postRequest("/system/basic/joblevel/", this.jl).then(resp => {
                        if (resp) {
                            this.initJls();
                            // 添加完成后，清空输入框
                            this.updateJl.name = '';
                            this.updateJl.titleLevel = '';
                        }else{
                            this.$message.error('添加失败');
                        }
                    })
                } else {
                    this.$message.error("添加字段不可以为空!");
                }
            },
            showEditView(data) {
                //Object.assign方法用于对象的合并,Object.assign(target, source1, source2);相同后面为准
                //白话->this.updateJl 一开始为空,是待提交的更新数据 ,data是原数据,通过assign()浅拷贝给了this.updateJl
                Object.assign(this.updateJl, data);
                this.dialogVisible = true;
            },
            doUpdate() {
                this.putRequest("/system/basic/joblevel/", this.updateJl).then(resp => {
                    if (resp) {
                        // 刷新表格
                        this.initJls();
                        // 添加完成后，清空输入框
                        this.updateJl.name = '';
                        this.updateJl.titleLevel = '';

                        this.dialogVisible = false
                    }
                })
            },


            deleteHandler(data) {
                this.$confirm('此操作将永久【' + data.name + '】职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/joblevel/" + data.id).then(resp => {
                        if (resp) {
                            // 删除成功后，刷新页面
                            this.initJls();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            initJls() {

                //初始化职称
                this.getRequest("/system/basic/joblevel/").then(resp => {
                    if (resp) {
                        //表格数据
                        this.jls = resp;
                        //待添加数据
                        this.jl = {
                            name: '',
                            titleLevel: ''
                        };
                    }
                })
            }
        }
    }
</script>

<style>

</style>